package com.tianze.library.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.tianze.library.R;

import butterknife.ButterKnife;

/**
 * Created by kingtami.
 */
public class UpdateDialog extends Dialog implements OnClickListener {

    TextView textViewChangeLog;
    TextView tvCancel;
    TextView tvConfirm;

    private String mCancelText;
    private String mConfirmText;
    private String mChangeLog;

    private OnSimpleClickListener mCancelClickListener;
    private OnSimpleClickListener mConfirmClickListener;

    public interface OnSimpleClickListener {
        void onClick(UpdateDialog simpleDialog);
    }

    public UpdateDialog(Context context) {
        super(context, R.style.dialog);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    public UpdateDialog setChangeLog(String text) {
        mChangeLog = text;
        if (textViewChangeLog != null && mChangeLog != null) {
            textViewChangeLog.setText(mChangeLog);
        }
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tiza_update_dialog);
        textViewChangeLog = ButterKnife.findById(this, R.id.textViewChangeLog);
        textViewChangeLog.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvCancel = ButterKnife.findById(this, R.id.textViewCancel);
        tvConfirm = ButterKnife.findById(this, R.id.textViewConfirm);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        setChangeLog(mChangeLog);
    }

    public UpdateDialog addCancelClickListener(OnSimpleClickListener listener) {
        mCancelClickListener = listener;
        return this;
    }

    public UpdateDialog addConfirmClickListener(OnSimpleClickListener listener) {
        mConfirmClickListener = listener;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.textViewConfirm) {
            if (mConfirmClickListener != null) {
                mConfirmClickListener.onClick(UpdateDialog.this);
            } else {
                UpdateDialog.super.dismiss();
            }
        } else if (v.getId() == R.id.textViewCancel) {
            if (mCancelClickListener != null) {
                mCancelClickListener.onClick(UpdateDialog.this);
            } else {
                UpdateDialog.super.dismiss();
            }
        }
    }

}
